<?php
/**
 * $Id: news.class.php 73 2005-12-01 21:32:12Z tito $
 *
 * FGS is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


class fgsProxys {

	/**
	 * getAll() return all proxys in database
	 * @return array of proxys
	 */
	function getAll() {
		$query = '
			SELECT *, UNIX_TIMESTAMP(dt_lastaccess) as date
			FROM fgs_fgsd
			ORDER BY fgsd_name';

		$sql = fgs::sql();
		$sql->query($query);
		return $sql->fetchAll();
	}

	/**
	 * getById() return a proxy
	 */
	function getById($id) {
		$query = sprintf('SELECT * FROM fgs_fgsd WHERE id = %d', $id);
		$sql = fgs::sql();
		$sql->query($query);
		if (!$sql->nbrows())
			return false;
		return $sql->getline();
	}

	/**
	 * add() add a proxy
	 */
	function add($name, $key, $password, $ip, $owner_id, $owner_name, $status, $blacklisted) {
		$query = sprintf('
			INSERT INTO fgs_fgsd
			SET 
				fgsd_name = "%s",
				fgsd_key = "%s",
				fgsd_pwd = "%s",
				fgsd_ip = "%s",
				fgsd_status = %d,
				fgsd_isblacklisted = %d,
				owner_id = %d,
				owner_name = "%s",
				dt_create = NOW()',
			fgsSql::escapeStr($name),
			fgsSql::escapeStr($key),
			fgsSql::escapeStr($password),
			fgsSql::escapeStr($ip),
			$status,
			$blacklisted,
			$owner_id,
			fgsSql::escapeStr($owner_name)
		);

		$sql = fgs::sql();
		return $sql->query($query) ? true : false;
	}

	/**
	 * update() update a proxy
	 */
	function update($id, $name, $key, $password, $ip, $owner_id, $owner_name, $status, $blacklisted) {
		$query = sprintf('
			UPDATE fgs_fgsd
			SET 
				fgsd_name = "%s",
				fgsd_key = "%s",
				fgsd_pwd = "%s",
				fgsd_ip = "%s",
				fgsd_status = %d,
				fgsd_isblacklisted = %d,
				owner_id = %d,
				owner_name = "%s",
				dt_create = NOW()
			WHERE id = %d',
			fgsSql::escapeStr($name),
			fgsSql::escapeStr($key),
			fgsSql::escapeStr($password),
			fgsSql::escapeStr($ip),
			$status,
			$blacklisted,
			$owner_id,
			fgsSql::escapeStr($owner_name),
			$id
		);

		$sql = fgs::sql();
		return $sql->query($query) ? true : false;
	}

	/**
	 * setBlacklisted() mark a proxy as blacklisted 
	 */
	function setBlacklisted($id, $blacklisted) {
		$query = sprintf('
			UPDATE fgs_fgsd
			SET 
				fgsd_isblacklisted = %d
			WHERE id = %d',
			$blacklisted ? 1 : 0,
			$id
		);

		$sql = fgs::sql();
		return $sql->query($query) ? true : false;
	}
	
	/**
	 * checkAuthorized() check if login/password exist
	 * @param key proxy key
	 * @param pwd proxy password (md5)
	 * @return array with proxy attributes, or false if failed.
	 */
	function checkAuthorized($key, $pwd) {
		
		// search if fgsd is authorized
		$query = sprintf('
			SELECT id, fgsd_name
			FROM fgs_fgsd
			WHERE
				fgsd_key = "%s"
				AND fgsd_pwd = "%s"
				AND NOT fgsd_isblacklisted',
				fgsSql::escapeStr($key),
				md5($pwd)
		);
		fgsLog::log(0, $query);

		$sql = fgs::sql();
		$sql->query($query);

		if ($sql->nbrows() != 1)
			return false;

		return $sql->getline();
	}
	
	/**
	 * checkSession() check if proxy session exist
	 * @param sid proxy session
	 * @return array with proxy attributes, or false if failed.
	 */
	function checkSession($sid) {
		
		// search if fgsd is authorized
		$query = sprintf('
			SELECT id, fgsd_name
			FROM fgs_fgsd
			WHERE
				fgsd_sid = "%s"
				AND fgsd_ip = "%s"
				AND NOT fgsd_isblacklisted',
				fgsSql::escapeStr($sid),
				fgsSql::escapeStr(fgsUtils::getIp())
		);

		$sql = fgs::sql();
		$sql->query($query);

		if ($sql->nbrows() != 1)
			return false;

		return $sql->getline();
	}

	/**
	 * checkSessionWithgame() check if proxy session exist with game session
	 * @param fgsd_sid proxy session
	 * @param game_sid game session
	 * @return array some info of proxy/game or false if failed
	 */
	function checkSessionWithGame($fgsd_sid, $game_sid) {

		// search if fgsd and game is authorized
		$query = sprintf('
			SELECT 
				ffg.id as ffg_id, 
				ffg.name,
				ff.id as ff_id, 
				ff.fgsd_name
			FROM fgs_fgsd_games AS ffg
			INNER JOIN fgs_fgsd AS ff
				ON ffg.id_fgsd = ff.id
			WHERE
				ff.fgsd_sid = "%s"
				AND ffg.sid = "%s"
				AND NOT ff.fgsd_isblacklisted',
			fgsSql::escapeStr($fgsd_sid),
			fgsSql::escapeStr($game_sid)
		);

		$sql = fgs::sql();
		$sql->query($query);

		if ($sql->nbrows() != 1)
			return false;

		return $sql->getline();
	}
	
	/**
	 * register() make proxy online
	 * @param key proxy key (login)
	 * @param key proxy password (md5)
	 * @return string proxy session
	 */
	function register($key, $pwd) {
	
		$fgsd = fgsProxys::checkAuthorized($key, $pwd);
		if ($fgsd === false)
			return false;

		// now, register proxy
		$fgsd_sid = md5(uniqid('s'));

		$query = sprintf('
			UPDATE fgs_fgsd
			SET
				fgsd_sid = "%s",
				fgsd_ip = "%s",
				fgsd_status = 1,
				dt_lastaccess = NOW()
			WHERE
				id = "%d"',
			fgsSql::escapeStr($fgsd_sid),
			fgsSql::escapeStr(fgsUtils::GetIp()),
			$fgsd['id']
		);

		$sql = fgs::sql();
		$sql->query($query);

		// TODO: verify success of last query
		fgsLog::log(0, sprintf('server "%s" is online', $fgsd['fgsd_name']));
		fgsLog::log(1, sprintf('server "%s" have SID=%s', $fgsd['fgsd_name'], $fgsd_sid));

		return $fgsd_sid;

	}

	/**
	 * unregister() make proxy online
	 * @param key proxy session
	 * @return bool true/false
	 */
	function unregister($sid) {
	
		$fgsd = fgsProxys::checkSession($sid);
		if ($fgsd === false)
			return false;

		$query = sprintf('
			UPDATE fgs_fgsd
			SET
				fgsd_sid = "",
				fgsd_status = 0,
				dt_lastaccess = NOW()
			WHERE
				id = "%d"',
			$fgsd['id']
		);

		$sql = fgs::sql();
		$sql->query($query);

		// TODO: verify success of last query
		fgsLog::log(0, sprintf('server "%s" is offline', $fgsd['fgsd_name']));

		return true;
	}

	/**
	 * ping() update last proxy access
	 * @param key proxy session
	 * @param ts proxy current timestamp (NOT USED YET)
	 * @return bool true/false
	 */
	function ping($sid, $ts) {
	
		$fgsd = fgsProxys::checkSession($sid);
		if ($fgsd === false)
			return false;

		$query = sprintf('
			UPDATE fgs_fgsd
			SET
				dt_lastaccess = NOW()
			WHERE
				id = "%d"',
			$fgsd['id']
		);

		$sql = fgs::sql();
		$sql->query($query);

		// TODO: verify success of last query
		fgsLog::log(0, sprintf('[%s] keepalive', $fgsd['fgsd_name']));

		return true;

	}

}

?>
